import datetime; now=datetime.date.today()

try:
    from gluon.contrib.gql import *
except:
    db=SQLDB('sqlite://db.db')
else:
    db=GQLDB()
    session.connect(request,response,db=db)

db.define_table("config",\
        SQLField("name"),\
        SQLField("appkey"),\
        SQLField("secret"),\
        SQLField("url"),\
        SQLField("v",'double')\
        )

db.define_table('person',
        db.Field('name','string'),
        )

db.define_table('category',
        SQLField('name'))

db.define_table('recipe',
        SQLField('title'),
        SQLField('email'),
        SQLField('description',length=256),
        SQLField('category',db.category),
        SQLField('date','date',default=now),
        SQLField('instructions','text'),
        SQLField('other'),
        )

db.category.name.requires=[IS_NOT_EMPTY(),IS_NOT_IN_DB(db,'category.name')]

db.recipe.title.requires=[IS_NOT_EMPTY()]
db.recipe.email.requires=IS_NULL_OR(IS_EMAIL())
db.recipe.description.requires=IS_NOT_EMPTY()
db.recipe.category.requires=IS_IN_DB(db,'category.id','category.name')
db.recipe.date.requires=IS_DATE()

db.config.name.requires=[IS_NOT_EMPTY(), IS_NOT_IN_DB(db,'config.name')]
db.config.appkey.requires=IS_NOT_EMPTY()
db.config.secret.requires=IS_NOT_EMPTY()
db.config.url.requires=[IS_NOT_EMPTY(), IS_HTTP_URL()]
db.config.v.requires=IS_NOT_EMPTY()
